package cn.com.bluemoon.daps.standard.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.common.toolkit.TreeUtils;
import cn.com.bluemoon.daps.standard.entity.DapSystemCategory;
import cn.com.bluemoon.daps.standard.service.DapSystemCategoryService;
import cn.com.bluemoon.daps.standard.vo.SystemCategoryTreeVo;
import cn.com.bluemoon.daps.standard.vo.TopicCategoryBaseVo;
import cn.com.bluemoon.daps.standard.vo.TopicCategoryVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 主题或分类定义（主题或分类、主题或分类、分类管理） 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-09-23
 */
@Api(tags = "主题定义-主题或分类管理")
@RestController
@Slf4j
@RequestMapping("/topicDefine/topicCategory")
@Validated
public class DapTopicDefineTopicCategoryController {

    @Autowired
    private DapSystemCategoryService categoryService;

    @GetMapping("/topic")
    @ApiOperation(value = "主题列表")
    @BmPermission(value = "standard:topicCategory:list", name = "主题列表", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = TopicCategoryVo.class, responseContainer = "List")})
    public ResultBean<List<TopicCategoryVo>> topicList() {
        List<DapSystemCategory> res = categoryService.list(categoryService.bmLambdaQuery().eq(DapSystemCategory::getTopic, 1));
        return ResultBean.ok(res).convert(c -> c.stream().map(TopicCategoryVo::new).collect(Collectors.toList()));
    }

    @GetMapping("/category/tree")
    @ApiOperation(value = "分类左树")
    @BmPermission(value = "standard:topicCategory:tree", name = "分类左树", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = TopicCategoryVo.class, responseContainer = "List")})
    public ResultBean<List<SystemCategoryTreeVo>> categoryTree() {
        List<DapSystemCategory> res = categoryService.list(categoryService.bmLambdaQuery().in(DapSystemCategory::getTopic, 0, 1));
        List<SystemCategoryTreeVo> treeVos = res.stream().map(SystemCategoryTreeVo::new).collect(Collectors.toList());
        List<SystemCategoryTreeVo> trees = TreeUtils.createTrees(treeVos, "id", "parentId", "nextNodes", null, null);
        // 第一层必须是主题
        List<SystemCategoryTreeVo> result = trees.stream().filter(d -> d.getTopic() == 1).collect(Collectors.toList());
        return ResultBean.ok(result);
    }

    @PostMapping("/category/list")
    @ApiOperation(value = "分类列表")
    @BmPermission(value = "standard:topicCategory:list", name = "分类列表", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = TopicCategoryVo.class, responseContainer = "List")})
    public ResultBean<List<TopicCategoryVo>> categoryList(@ApiParam("分类中左树所选分类id") @RequestParam(value = "categoryId") String categoryId) {
        List<DapSystemCategory> res = categoryService.list(categoryService.bmLambdaQuery()
                .ne(DapSystemCategory::getTopic, 1).eq(DapSystemCategory::getParentId, categoryId));
        return ResultBean.ok(res).convert(c -> c.stream().map(TopicCategoryVo::new).collect(Collectors.toList()));
    }

    @PostMapping
    @ApiOperation(value = "主题或分类新增")
    @BmPermission(value = "standard:topicCategory:add", name = "主题或分类新增", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = String.class)})
    public ResultBean<String> topicCategorySave(@Valid @RequestBody TopicCategoryBaseVo categoryVo,
                                                @ApiParam("若为分类新增，则入参为分类中左树所选分类id；若为主题新增，则不需要该入参") @RequestParam(value = "categoryId", required = false) String categoryId) {
        DapSystemCategory topicCategoryBo = categoryVo.toModel();
        topicCategoryBo.setParentId(categoryId);
        DapSystemCategory topicCategory = categoryService.saveOrUpdateSysCategory(topicCategoryBo);
        return ResultBean.ok(topicCategory.getId());
    }

    @PutMapping("/{topicCategoryId}")
    @ApiOperation(value = "主题或分类更新")
    @BmPermission(value = "standard:topicCategory:edit", name = "主题或分类更新", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Integer.class)})
    public ResultBean<String> topicCategoryUpdate(@Valid @RequestBody TopicCategoryBaseVo topicCategoryVo, @PathVariable String topicCategoryId) {
        BmAssetUtils.notNull(topicCategoryId, "主题或分类id不可为空");
        DapSystemCategory topicCategory = topicCategoryVo.toModel();
        topicCategory.setId(topicCategoryId);
        DapSystemCategory oldBo = categoryService.getByIdAsset(topicCategoryId, "主题/分类不存在");
        String parentId = oldBo.getParentId();
        topicCategory.setParentId(parentId);
        DapSystemCategory topicCategoryRes = categoryService.saveOrUpdateSysCategory(topicCategory);
        return ResultBean.ok(topicCategoryRes.getId());
    }

    @DeleteMapping("/{topicCategoryId}")
    @ApiOperation(value = "主题或分类删除")
    @BmPermission(value = "standard:topicCategory:delete", name = "主题或分类删除", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = Boolean.class)})
    public ResultBean<Boolean> topicCategoryDelete(@PathVariable String topicCategoryId) {
        boolean remove = categoryService.deleteTopicCategoryById(topicCategoryId);
        return ResultBean.ok(remove);
    }

    @GetMapping("/{topicCategoryId}")
    @ApiOperation(value = "主题或分类详情")
    @BmPermission(value = "standard:topicCategory:get", name = "主题或分类详情", ignore = false)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = TopicCategoryVo.class)})
    public ResultBean<TopicCategoryVo> topicCategoryOne(@PathVariable String topicCategoryId) {
        DapSystemCategory topicCategory = categoryService.getByIdAsset(topicCategoryId, "主题/分类不存在");
        return ResultBean.ok(topicCategory).convert(TopicCategoryVo::new);
    }


}
